﻿using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Engine;
using Game.NetWork;
using GameEntitySystem;
using TemplatesDatabase;
using Test1;
using MFTrain;
using NK;

namespace Game
{
    public class ComponentTask : Component, IUpdateable
    {
        public static List<Task> m_alltasks = new List<Task>();

        public SubsystemWorldSystem m_subsystemWorldSystem;

        public ComponentPlayer m_componentPlayer;

        public ComponentPlayerSystem m_componentPlayerSystem;

        public int MainTaskIndex;

        public bool MainTaskConditions;

        public Task tasknow;

        public bool showmessage = false;

        public bool Lordtask1;

        public bool BloodPool;

        public bool Dodo1;

        public bool City1;

        public bool YS;

        private ContainerWidget m_screenLabelCanvasWidget;

        private float m_screenLabelCloseTime;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public Dictionary<string, Task> m_othertasks => m_subsystemWorldSystem.GetPlayerInfo(m_componentPlayer.PlayerData.PlayerIndex).m_othertasks;

        public void Update(float dt)
        {
            tasknow = GetTask(MainTaskIndex);
            if (m_componentPlayer.PlayerStats.DistanceTravelled >= 100.0 && MainTaskIndex == 0)
            {
                FinishedTask(null, TaskType.Main);
            }
            else if (BloodPool && MainTaskIndex == 1)
            {
                FinishedTask(null, TaskType.Main);
            }
            else if (Dodo1 && MainTaskIndex == 2)
            {
                FinishedTask(null, TaskType.Main);
            }
            else if (Lordtask1 && MainTaskIndex == 3)
            {
                FinishedTask(null, TaskType.Main);
            }
            else if (City1 && MainTaskIndex == 4)
            {
                FinishedTask(null, TaskType.Main);
            }
            else if (YS && MainTaskIndex == 5)
            {
                FinishedTask(null, TaskType.Main);
            }

            if (m_screenLabelCloseTime > 0f)
            {
                m_screenLabelCloseTime -= dt;
                if (m_screenLabelCloseTime < 0.1f)
                {
                    m_screenLabelCanvasWidget.IsVisible = false;
                    m_screenLabelCloseTime = 0f;
                }
            }
        }

        public void FinishedTask(string name, TaskType taskType)
        {
            if (taskType == TaskType.Other)
            {
                m_othertasks[name].Conditions = true;
                return;
            }

            MainTaskConditions = true;
            if (Time.PeriodicEvent(1.0, 0.0) && CommonLib.WorkType == WorkType.Client && CommonLib.MainPlayer == m_componentPlayer)
            {
                IPackage package = new ComponentTaskPackage(this, ComponentTaskPackage.EventType.SyncStat);
                CommonLib.Net.QueuePackage(package);
            }
        }

        public void AddTask(string name, bool conditions)
        {
            Task task = GetTask(name, TaskType.Other);
            task.Conditions = conditions;
            Log.Information("add" + name);
            m_othertasks.Add(name, task);
        }

        public void RemoveTask(Task task)
        {
            try
            {
                if (task.TaskType == TaskType.Other)
                {
                    m_othertasks.Remove(task.Name);
                    return;
                }

                MainTaskIndex++;
                MainTaskConditions = false;
                Log.Information("移除任务已经执行");
                if (CommonLib.WorkType == WorkType.Client && CommonLib.MainPlayer == m_componentPlayer)
                {
                    IPackage package = new ComponentTaskPackage(this, ComponentTaskPackage.EventType.SyncStat);
                    CommonLib.Net.QueuePackage(package);
                }
            }
            catch (Exception ex)
            {
                Log.Information(ex.ToString());
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(throwOnError: true);
            m_componentPlayerSystem = base.Entity.FindComponent<ComponentPlayerSystem>(throwOnError: true);
            m_subsystemWorldSystem = base.Project.FindSubsystem<SubsystemWorldSystem>(throwOnError: true);
            MainTaskIndex = valuesDictionary.GetValue("MainTaskIndex", 0);
            MainTaskConditions = valuesDictionary.GetValue("MainTaskConditions", defaultValue: false);
            InitScreenLabelCanvas();
            Lordtask1 = valuesDictionary.GetValue("Lordtask", defaultValue: false);
            BloodPool = valuesDictionary.GetValue("BloodPool", defaultValue: false);
            Dodo1 = valuesDictionary.GetValue("Dodo1", defaultValue: false);
            City1 = valuesDictionary.GetValue("City1", defaultValue: false);
            YS = valuesDictionary.GetValue("YS", defaultValue: false);
            m_subsystemWorldSystem = base.Project.FindSubsystem<SubsystemWorldSystem>(throwOnError: true);
            if (m_alltasks.Count != 0)
            {
                m_alltasks.Clear();
            }

            if (m_alltasks.Count == 0)
            {
                m_alltasks.Add(new Task
                {
                    
                });

               
            }
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("MainTaskIndex", MainTaskIndex);
            valuesDictionary.SetValue("MainTaskConditions", MainTaskConditions);
            valuesDictionary.SetValue("Lordtask1", Lordtask1);
            valuesDictionary.SetValue("BloodPool", BloodPool);
            valuesDictionary.SetValue("Dodo1", Dodo1);
            valuesDictionary.SetValue("City1", City1);
            valuesDictionary.SetValue("YS", YS);
        }

        public void InitScreenLabelCanvas()
        {
            XElement node = ContentManager.Get<XElement>("Widgets/TextLabelWidget");
            m_screenLabelCanvasWidget = (ContainerWidget)Widget.LoadWidget(m_componentPlayer.GameWidget, node, null);
            m_screenLabelCanvasWidget.IsVisible = false;
            m_componentPlayer.GameWidget.Children.Add(m_screenLabelCanvasWidget);
        }

        public void OpenText(string Text1, string entityname)
        {
            Vector2 vector = (-330f, -150f);
            m_screenLabelCanvasWidget.IsVisible = true;
            m_screenLabelCloseTime = 8f;
            Vector2 size = (300f, 150f);
            CanvasWidget canvasWidget = (CanvasWidget)m_screenLabelCanvasWidget;
            canvasWidget.Size = size;
            int num = (int)(m_componentPlayer.GameWidget.ActualSize.X / 2f) - (int)(size.X / 2f);
            int num2 = (int)(m_componentPlayer.GameWidget.ActualSize.Y / 2f) - (int)(size.Y / 2f);
            m_componentPlayer.GameWidget.SetWidgetPosition(m_screenLabelCanvasWidget, new Vector2(num, num2) + vector);
            LabelWidget labelWidget = m_screenLabelCanvasWidget.Children.Find<LabelWidget>("Content");
            labelWidget.Text = entityname + ":" + Text1;
            labelWidget.Color = new Color(255, 255, 255);
            labelWidget.FontScale = 1f;
            RectangleWidget rectangleWidget = m_screenLabelCanvasWidget.Children.Find<RectangleWidget>("ScreenLabelRectangle");
            rectangleWidget.FillColor = new Color(20, 20, 20, 130);
            rectangleWidget.OutlineColor = Color.DarkRed;
            rectangleWidget.OutlineThickness = 4f;
        }

        public static Task GetTask(string name, TaskType taskType)
        {
            foreach (Task alltask in m_alltasks)
            {
                if (alltask.Name == name && alltask.TaskType == taskType)
                {
                    return alltask;
                }
            }

            return GetBaseTask();
        }

        public static Task GetBaseTask()
        {
            return new Task
            {
                Name = "前面的章节以后再来探索吧！",
                Text = "恭喜你成功达成了本次版本所有的任务，您可以接取副线任务，或者继续游玩下去",
                RewardsText = "没有任何奖励可以领了",
                TaskType = TaskType.Main,
                Index = 0,
                Rewards = delegate
                {
                }
            };
        }

        public static Task GetTask(int index)
        {
            foreach (Task alltask in m_alltasks)
            {
                if (alltask.Index == index)
                {
                    return alltask;
                }
            }

            return GetBaseTask();
        }
    }
}